import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { getUserInfo, updateUserInfo, updateUserPassword, updateAvatar, updateNewPassword } from '@/api/user'
import { ElMessage } from 'element-plus'

export const useUserProfileStore = defineStore('userProfile', () => {
  // 用户信息
  const userInfo = ref({})
  // 加载状态
  const loading = ref(false)
  
  // 获取用户信息
  async function fetchUserInfo() {
    loading.value = true
    try {
      const response = await getUserInfo()
      if (response.status === 0) {
        userInfo.value = response.data
        return response.data
      }
      return null
    } catch (error) {
      console.error('获取用户信息失败:', error)
      ElMessage.error('获取用户信息失败')
      return null
    } finally {
      loading.value = false
    }
  }
  
  // 更新用户信息
  async function updateProfile(data) { 
    loading.value = true
    try {
      const response = await updateUserInfo(data)
      if (response.status === 0) {
        userInfo.value = {...userInfo.value, ...data}
        localStorage.removeItem('token')
        localStorage.setItem('token', response.data)
        ElMessage.success('个人资料更新成功')
        return true
      }
      ElMessage.error(response.message)
      return false
    } catch (error) {
      console.error('更新用户信息失败:', error)
      return false
    } finally {
      loading.value = false
    }
  }

  // 更新用户密码
  async function updatePassword(data) {
    loading.value = true
    try {
      const response = await updateUserPassword(data)
      if (response.status === 0) {
        ElMessage.success('密码更新成功')
        return true
      }
      ElMessage.error(response.message)
      return false
    } catch (error) {
      console.error('更新密码失败:', error)
      return false
    } finally {
      loading.value = false
    }
  }

  // 更新用户头像
  async function updateUserAvatar(data) {
    loading.value = true
    try {
      const response = await updateAvatar(data)
      if (response.status === 0) {
        fetchUserInfo()
        ElMessage.success('头像更新成功')
        return true
      }
      ElMessage.error(response.message)
      return false
    } catch (error) {
      console.error('更新头像失败:', error)
      ElMessage.error('更新头像失败')
      return false
    } finally {
      loading.value = false
    }
  }

  // 判断用户是否已设置密码
  function hasPassword() {
    return userInfo.value.password !== "-1"
  }

  // 设置用户密码
  async function setPassword(data) {
    loading.value = true
    try {
      const response = await updateNewPassword(data)
      console.log(response)
      
      if (response.status === 0) {
        ElMessage.success('密码设置成功')
        return true
      }
      ElMessage.error(response.message)
      return false
    } catch (error) {
      console.error('设置密码失败:', error)
      return false
    } finally {
      loading.value = false
    }
  } 

  // 清除用户信息
  function clearUserInfo() {
    userInfo.value = {}
  }
  
  return {
    userInfo,
    loading,
    fetchUserInfo,
    updateProfile,
    updatePassword,
    updateUserAvatar,
    clearUserInfo,
    hasPassword,
    setPassword
  }
}, {
  persist: {
    key: 'user-profile',
    storage: localStorage,
    paths: ['userInfo']
  }
}) 